package com.guldan.jaina.manager;

import com.guldan.core.exception.BusinessException;
import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.feedback.FeedbackDTO;
import com.guldan.jaina.dto.feedback.FeedbackSearchDTO;
import com.guldan.jaina.utils.FeedbackDomainConvertUtils;
import com.guldan.mapper.db.entity.Feedback;
import com.guldan.mapper.db.entity.FeedbackExample;
import com.guldan.mapper.db.mapper.FeedbackMapperExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2017/2/22
 * Time: 11:46
 */
@Service
public class FeedbackManager extends BaseManager {

    @Autowired
    private FeedbackMapperExt feedbackMapperExt;

    //获取单个反馈信息
    public FeedbackDTO get(Integer areaId) {
        Feedback feedback = feedbackMapperExt.selectByPrimaryKey(areaId);
        if (feedback != null) {
            FeedbackDTO dto = FeedbackDomainConvertUtils.convert(feedback);
            return dto;
        }
        return null;
    }


    //新增/更新单个反馈，传入：AreaDTO
    public FeedbackDTO save(FeedbackDTO dto) throws BusinessException {

        Feedback entity;
        if (dto.getId() != null && dto.getId() > 0) {
            entity = feedbackMapperExt.selectByPrimaryKey(dto.getId());
            entity.setType(dto.getType());
            entity.setSource(dto.getSource());
            entity.setTitle(dto.getTitle());
            entity.setName(dto.getName());
            entity.setTel(dto.getTel());
            entity.setCompany(dto.getCompany());
            entity.setContent(dto.getContent());
            entity.setState(dto.getState());

            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            feedbackMapperExt.updateByPrimaryKeySelective(entity);
        } else {

            entity = new Feedback();
            entity.setType(dto.getType());
            entity.setSource(dto.getSource());
            entity.setTitle(dto.getTitle());
            entity.setName(dto.getName());
            entity.setTel(dto.getTel());
            entity.setCompany(dto.getCompany());
            entity.setContent(dto.getContent());
            entity.setState(dto.getState());

            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            feedbackMapperExt.insertSelective(entity);
        }
        dto = FeedbackDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个反馈信息
    public Boolean delete(Integer areaId) {
        Feedback entity = feedbackMapperExt.selectByPrimaryKey(areaId);
        entity.setIsDelete(true);
        return feedbackMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //分页查询反馈信息列表
    public Pagination<FeedbackDTO> list(FeedbackSearchDTO feedbackSearchDTO) {


        Pagination<FeedbackDTO> result = new Pagination<FeedbackDTO>(feedbackSearchDTO.getCurrentPage(), feedbackSearchDTO.getPerPage());
        FeedbackExample example = buildSearchExample(feedbackSearchDTO);
        int total = feedbackMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<FeedbackDTO> data = new ArrayList<FeedbackDTO>();
            List<Feedback> list = feedbackMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (Feedback entity : list) {
                FeedbackDTO dto = FeedbackDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    //build查询Example
    private FeedbackExample buildSearchExample(FeedbackSearchDTO search) {
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(search.getTitle())) {
            criteria.andTitleLike("%" + search.getTitle() + "%");
        }
        if (search.getSource() != null && search.getState() > 0) {
            criteria.andSourceEqualTo(search.getSource());
        }
        if (search.getState() != null && search.getState() > 0) {
            criteria.andStateEqualTo(search.getState());
        }
        if (search.getType() != null && search.getType() >= 0) {
            criteria.andTypeEqualTo(search.getType());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("id asc");
        }
        return example;
    }

}
